package com.logic.modular.gameCore.cultivate.task.service;


import com.logic.modular.gameCore.cultivate.task.mapper.CoreTaskConfigMapper;
import lombok.extern.slf4j.Slf4j;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.comment.pojo.domain.logic.core.CoreTaskConfig;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;


/**
 * (CoreTaskConfig)表服务实现类
 *
 * @author cxy
 * @since 2024-08-09 14:20:04
 */
@Slf4j
@Service

public class CoreTaskConfigServiceImpl extends ServiceImpl<CoreTaskConfigMapper, CoreTaskConfig>{
    private static final Cache<String, Map<Long,CoreTaskConfig>> cache = Caffeine.newBuilder().build();
    
    @PostConstruct
    private void init() {
        CoreTaskConfig initSelect = CoreTaskConfig.builder().build();
        List<CoreTaskConfig> list = this.list(new LambdaQueryWrapper<>(initSelect));
        log.info("开始缓存数据,数据量为:{}",list.size());
        cache.put(
                "coreTaskConfig", 
                list.stream().collect(Collectors.toMap(CoreTaskConfig::getId, CoreTaskConfig::init))
        );
    }

    public static List<CoreTaskConfig> listCache() {
        Map<Long,CoreTaskConfig> values = cache.getIfPresent("coreTaskConfig");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return new ArrayList<>(values.values());
    }     
    
    public Map<Long,CoreTaskConfig> mapCache() {
        Map<Long,CoreTaskConfig> values = cache.getIfPresent("coreTaskConfig");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values;
    }   
    
    public CoreTaskConfig getCache(Long id) {
        Map<Long,CoreTaskConfig> values = cache.getIfPresent("coreTaskConfig");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values.get(id);
    }
}
